/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var DataFrame = new Class({    
    
    Implements: [Options,Events],
    
    options: {
        id       : '',
        isMaster : false
    },
    
    sources      : new Array(),
    pointer      : 0,
    slidingDoors : null,
    container    : null,
    
    initialize: function(options){
        this.setOptions(options);
    },
    
    getCurrentSource: function(){
        return this.sources[this.pointer];
    },
    
    shift: function(){
      this.pointer = ++this.pointer%this.sources.length;  
    },
    
    addSource: function(source){
        this.sources[this.sources.length] = Object.clone(source);
    },
    
    getId: function(){
        return this.options.id;
    },
    
    getHtmlId: function(){
        return this.options.htmlId;
    },
    
    isMaster: function(){
        return this.options.isMaster;
    },
    
    prepareLoop: function(el){
        this.options.isMaster = el.hasClass('master');
        this.slidingDoors = new FlipingDoors({
            'el' : el
        });
        Array.each(this.sources, function(source,key){
            source.setSlidingDoors(this.slidingDoors);
        }.bind(this));
        this.container = el;
    },
    
    startLoop: function(){
        
        this.pointer = 0;
        Array.each(this.sources, function(source,key){
            source.updateRevision();
        }.bind(this));
        var loop = function(source){  
            source.removeEvents();
            source.addEvent('timelineEnd',function(source){
                source.removeEvents('timelineEnd');
                if(source != this.getCurrentSource()){
                    return;
                }
                this.shift();
                if(this.pointer == 0){
                    source.stopLoop();  
                    this.fireEvent('timelineEnd',this);
                }else{
                    loop(this.getCurrentSource());
                }                
            }.bind(this));
            source.startLoop(this.container);
        }.bind(this);
        if(this.sources.length > 0)
            loop(this.sources[this.pointer]); 
    },
    
    stopLoop: function(){
        //console.log('stopLoop');
        Array.each(this.sources, function(source,key){
            source.stopLoop();
            //source.removeEvents();
        });
    }
    
});


